//********
/*!

   \file

   © 2011 Mark W. Gabby II
*/
#include "WindowsSpecific.hpp"

#include "Platform.hpp"

extern HINSTANCE gInstanceHandle;

static pf::tWindowID gNextID = 0;

ScopedOpenGLContext::ScopedOpenGLContext()
{
}

void ScopedOpenGLContext::Init( const uint32_t PixelFormatIndex )
{
  	WNDCLASS	WindowClass;
   WindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   WindowClass.lpfnWndProc = DefWindowProc;
   WindowClass.cbClsExtra = 0;
   WindowClass.cbWndExtra = 0;
   WindowClass.hInstance = gInstanceHandle;
   WindowClass.hIcon = NULL;
   WindowClass.hCursor = 0;
   WindowClass.hbrBackground = NULL;
   WindowClass.lpszMenuName = NULL;
   WindowClass.lpszClassName = "ScopedOpenGLContextWindowClass";

   // Register the window class
   RegisterClass(&WindowClass);
   UINT UIStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
	windowHandle = CreateWindow( WindowClass.lpszClassName, "ScopedOpenGLContextWindow", UIStyle,
                             0, 0, 1, 1, NULL, NULL, gInstanceHandle, NULL);
	HDC DeviceContext = GetDC();

   // Create a simple pixel format desired.
   PIXELFORMATDESCRIPTOR pfd = { 0 };
   pfd.nSize = sizeof( pfd );
   pfd.nVersion = 1;
   pfd.cColorBits = 32;
   pfd.cDepthBits = 24;
   pfd.cAccumBits = 0;
   pfd.cStencilBits = 8;
   pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;

   if ( PixelFormatIndex != kNull32 )
   {
      SetPixelFormat( DeviceContext, PixelFormatIndex, &pfd );
   }
   else
   {
      uint32_t tempPFindex = static_cast<uint32_t>( ChoosePixelFormat( DeviceContext, &pfd ) );
      SetPixelFormat( DeviceContext, tempPFindex, &pfd );
   }
   // Bind OpenGL to the window.
   openGLContextHandle = wglCreateContext( DeviceContext );
   wglMakeCurrent( DeviceContext, openGLContextHandle );

   ReleaseDC( DeviceContext );
}

ScopedOpenGLContext::~ScopedOpenGLContext()
{
   // Now we're done with the OpenGL rendering context,
   // so get rid of it.
   // If it's current, then
   if ( wglGetCurrentContext() == openGLContextHandle )
   {
      HDC DeviceContext = GetDC();
      // make it not current
      wglMakeCurrent( DeviceContext, NULL );
      ReleaseDC( DeviceContext );
   }
   wglDeleteContext( openGLContextHandle );

   // Cleanup: Destroy the window and unregister the window class
   DestroyWindow( windowHandle );
   UnregisterClass( "ScopedOpenGLContextWindowClass", gInstanceHandle );
}

bool ScopedOpenGLContext::IsCurrent()
{
   return wglGetCurrentContext() == openGLContextHandle;
}

void ScopedOpenGLContext::MakeCurrent()
{
   if ( IsCurrent() == false )
   {
      HDC DeviceContext = GetDC();
      wglMakeCurrent( DeviceContext, openGLContextHandle );
      ReleaseDC( DeviceContext );
   }
   else
   {
      // Do nothing, already current
   }
}
